package maple.core.contextproviders;

import java.util.Vector;

import maple.core.ContextProvider;
import maple.lang.Language;
import maple.util.Context;
import maple.util.Sensor;

public class ContextProviderTemplate extends ContextProvider {
    private double _threshold;

    public ContextProviderTemplate() {
        super(1);

        // from Consumer
        _preconditions.add(Sensor.Type.PHIDGET_MOTION.toString()); // PHIDGET_MOTION="doorSensor,deskSensor"
        _postcondition = Context.Type.HUMANPRESENCE.toString();
    }

    protected void setup() {
        super.setup();

        _threshold = Double.parseDouble(_arguments[1]);
    }

    protected String interpret() {
        // PHIDGET_MOTION="doorSensor,deskSensor"
        String[] sensorReadings = _perceptions.get(_preconditions.get(0)).split(Language.COMMA);

        double doorSensorReading = Double.parseDouble(Sensor.toString(_preconditions.get(0), sensorReadings[0]));
        double deskSensorReading = Double.parseDouble(Sensor.toString(_preconditions.get(0), sensorReadings[1]));

        if ((doorSensorReading < 0) || (deskSensorReading < 0)) {
            return ContextProvider.State.UNKNOWN.toString();
        } else if ((doorSensorReading > _threshold) || (deskSensorReading > _threshold)) {
            return Context.HumanPresence.TRUE.toString();
        } else {
            return Context.HumanPresence.FALSE.toString();
        }
    }
}
